﻿using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;

namespace LTMCompanyName.YoyoCmsTemplate.Enums
{
    /// <summary>
    /// 枚举扩展函数
    /// </summary>
    public static class EnumExtension
    {
        /// <summary>
        /// 根据枚举转换成 SelectList ，用于绑定下拉列表
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <param name="enumObj"></param>
        /// <param name="markCurrentAsSelected">是否选择当前项</param>
        /// <param name="showAll">是否显示"全部" 与 showChoose 互斥</param>
        /// <param name="showChoose">是否显示"请选择" 与 showAll 互斥</param>
        /// <param name="valuesToExclude"></param>
        /// <returns></returns>
        public static SelectList ToSelectList<TEnum>(this TEnum enumObj,
            bool markCurrentAsSelected = true, bool showAll = true, bool showChoose = false, int[] valuesToExclude = null) where TEnum : struct
        {
            if (!typeof(TEnum).IsEnum) throw new ArgumentException("枚举类型不能为空", "enumObj");

            var values = from TEnum enumValue in Enum.GetValues(typeof(TEnum))
                         where valuesToExclude == null || !valuesToExclude.Contains(Convert.ToInt32(enumValue))
                         select new { ID = Convert.ToInt32(enumValue), Name = EnumExtension.GetDescription(enumValue) };

            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var item in values)
                list.Add(new SelectListItem() { Text = item.Name, Value = item.ID.ToString() });

            if (showAll)
                list.Insert(0, new SelectListItem() { Text = "全部", Value = "0" });
            else if (showChoose)
                list.Insert(0, new SelectListItem() { Text = "请选择", Value = "0" });

            object selectedValue = null;
            if (markCurrentAsSelected)
                selectedValue = Convert.ToInt32(enumObj);
            return new SelectList(list, "Value", "Text", selectedValue);
        }
        public static SelectList ToSelectList2<TEnum>(this TEnum enumObj,
bool markCurrentAsSelected = true, bool showAll = true, int[] valuesToExclude = null, string selectedvalue = null) where TEnum : struct
        {
            if (!typeof(TEnum).IsEnum) throw new ArgumentException("枚举类型不能为空", "enumObj");

            var values = from TEnum enumValue in Enum.GetValues(typeof(TEnum))
                         where valuesToExclude == null || !valuesToExclude.Contains(Convert.ToInt32(enumValue))
                         select new { ID = Convert.ToInt32(enumValue), Name = EnumExtension.GetDescription(enumValue) };

            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var item in values)
                list.Add(new SelectListItem() { Text = item.Name, Value = item.Name });

            if (showAll)
                list.Insert(0, new SelectListItem() { Text = "全部", Value = "0" });

            object selectedValue = null;
            if (markCurrentAsSelected)
                selectedValue = Convert.ToInt32(enumObj);
            if (selectedvalue != null && list.Any(a => a.Value == selectedvalue))
            {
                selectedValue = selectedvalue;
            }
            return new SelectList(list, "Value", "Text", selectedValue);
        }
        public static SelectList ToSelectList3<TEnum>(this TEnum enumObj,
bool markCurrentAsSelected = true, bool showAll = true, int[] valuesToExclude = null, string selectedvalue = null) where TEnum : struct
        {
            if (!typeof(TEnum).IsEnum) throw new ArgumentException("枚举类型不能为空", "enumObj");

            var values = from TEnum enumValue in Enum.GetValues(typeof(TEnum))
                         where valuesToExclude == null || !valuesToExclude.Contains(Convert.ToInt32(enumValue))
                         select new { ID = Convert.ToInt32(enumValue), Name = EnumExtension.GetDescription(enumValue) };

            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var item in values)
                list.Add(new SelectListItem() { Text = item.Name, Value = item.ID.ToString() });

            if (showAll)
                list.Insert(0, new SelectListItem() { Text = "全部", Value = "0" });

            object selectedValue = null;
            if (markCurrentAsSelected)
                selectedValue = Convert.ToInt32(enumObj);
            if (selectedvalue != null && list.Any(a => a.Value == selectedvalue))
            {
                selectedValue = selectedvalue;
            }
            return new SelectList(list, "Value", "Text", selectedValue);
        }
        /// <summary>
        /// 获取指定枚举的描述信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static string GetDescription<T>(this T enumValue)
          where T : struct
        {
            return GetEnumDescription(enumValue);
        }

        /// <summary>
        /// 获取枚举的描述值
        /// </summary>
        /// <param name="enumItem"></param>
        /// <returns></returns>
        public static string GetEnumDescription(object enumItem)
        {
            string str = "";
            try
            {
                DescriptionAttribute[] des = (DescriptionAttribute[])enumItem.GetType().GetField(enumItem.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (des != null && des.Count() > 0)
                {
                    str = des[0].Description;
                }
                else
                {
                    str = enumItem.ToString();
                }
            }
            catch (Exception) { }
            return str;
        }


        public static int GetIndex(List<string> collection, string value)
        {
            int result = 0;
            for (int i = 0; i < collection.Count; i++)
            {
                if (collection[i] == value)
                {
                    result = i;
                    break;
                }
            }
            return result;
        }

        public static int GetIndex(DataTable table, string key)
        {
            int result = 0;
            for (int i = 0; i < table.Rows.Count; i++)
            {
                if (table.Rows[i]["id"].ToString() == key)
                {
                    result = i;
                    break;
                }
            }
            return result;
        }

        public static List<SelectListItem> ToList<TEnum>(this TEnum enumObj) where TEnum : struct
        {
            if (!typeof(TEnum).IsEnum) throw new ArgumentException("枚举类型不能为空", "enumObj");
            var values = from TEnum enumValue in Enum.GetValues(typeof(TEnum)) select new { ID = Convert.ToInt32(enumValue), Name = EnumExtension.GetDescription(enumValue) };
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var item in values)
                list.Add(new SelectListItem() { Text = item.Name, Value = item.ID.ToString() });
            return list;
        }
    }
}
